{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def factorial(n):\n",
    "    i = 1\n",
    "    s = 1\n",
    "    while(i <= n):\n",
    "        s = s * i\n",
    "        i = i + 1\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factorial(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def m_cos(x, d=0.01):\n",
    "    n = 1   # 注意数学中n的起始一般为1，但这样代码中会略显复杂，所以我们以0开始\n",
    "    s = 0\n",
    "    sn = d\n",
    "    while (abs(s - sn) >= d):\n",
    "        sn = s\n",
    "        m = (n-1)\n",
    "        s = s + (-1)**m * (x**(2*m)) / factorial(2*m)\n",
    "        n = n + 1\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cos_np(x, d=1E-9):\n",
    "    n = 0\n",
    "    s = 0\n",
    "    last = d\n",
    "    while (abs(s - last) >= d):\n",
    "        last = s\n",
    "        s = s + (-1)**n * (x**(2*n)) / factorial(2*n)\n",
    "        n = n + 1\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.9999987317288787"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 3.14\n",
    "cos_np(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.9999987317275395"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.cos(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sin_np(x, d=1E-9):\n",
    "    n = 1\n",
    "    s = 0\n",
    "    last = d\n",
    "    while (abs(s - last) >= d):\n",
    "        last = s\n",
    "        s = s + (-1)**(n - 1) * (x**(2*n - 1)) / factorial(2*n - 1)\n",
    "        n = n + 1\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4999999999870039"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2 = 3.1415926535/6\n",
    "sin_np(x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4999999999870395"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.sin(x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "79dbfe2302debfe5b3e74c370ab328d0e0a721b471281615b2e35d9aabd1e1c8"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
